<?php

/**
 * Created by PhpStorm.
 * User: panya
 * Date: 2017/7/23
 * Time: 下午8:13
 */
class wx_reg extends CI_Controller
{
    //微信验证
    function wx_login()
    {
        $code = $this->input->get('code');
        if ($code == '') {
            echo 'code:001';
        } else {
            $a = file_get_contents('https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->config->item('wxAppId') . '&secret=' . $this->config->item('wxAppSecret') . '&code=' . $code . '&grant_type=authorization_code');
            $access_token = json_decode($a, true);

            $a = file_get_contents('https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token['access_token'] . '&openid=' . $access_token['openid'] . '&lang=zh_CN');
            $m = json_decode($a, true);

            $da['subscribe'] = 1;
            $da['openid'] = $m['openid'];
            $da['nickname'] = $m['nickname'];
            $da['sex'] = $m['sex'];
            $da['city'] = $m['city'];
            $da['country'] = $m['country'];
            $da['province'] = $m['province'];
            $da['language'] = $m['language'];
            $da['headimgurl'] = $m['headimgurl'];


            $q = $this->db->get_where('member', array('openid' => $da['openid']));
            if ($q->num_rows() > 0 && $da['nickname']) {
//更新数据
                $this->db->update('member', $da, array('openid' => $da['openid']));

            } else {
                $da['tj_mid'] = $this->config->item('tj_mid');
                $da['subscribe_time'] = mktime();

                $this->db->insert('member', $da);
                $da['mid'] = $this->db->insert_id();
            }

            if ($da['mid']) {
                $s['mid'] = $da['mid'];
                $s['min'] = TRUE;
                $s['head'] = $da['headimgurl'];
                $this->session->set_userdata($s);

                $this->pub->logq('微信验证');
                if ($this->session->userdata('ls_url')) {
                    redirect($this->session->userdata('ls_url'));
                } else {
                    redirect('/');
                }
            } else {
                echo 'code:002';
            }
        }
    }

    //获取支付的openid
    function up_openid_pay()
    {
        $this->pub->logq('获取支付openid ');
        $code = $this->input->get('code');
        if ($code == '') {
            echo 'code:001';
        } else {
            //当前使用的支付版本
            $pay_c = $this->config->item('pay_c');
            $pay = $this->config->item('pay');
            $a = file_get_contents('https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $pay[$pay_c]['wxAppId'] . '&secret=' . $pay[$pay_c]['wxAppSecret'] . '&code=' . $code . '&grant_type=authorization_code');
            $access_token = json_decode($a, true);

            $this->db->where('openid', $access_token['openid']);
            $q = $this->db->get_where('openid');
            if ($q->num_rows() == 0) {
                $da['mid'] = $this->session->userdata('mid');
                $da['type'] = 1;
                $da['edition'] = $pay_c;
                $da['openid'] = $access_token['openid'];
                $da['mktime'] = mktime();
                $this->db->insert('openid', $da);
            }

            if ($this->session->userdata('ls_url')) {
                redirect($this->session->userdata('ls_url'));
            } else {
                redirect('/');
            }
        }
    }

//获取发红包openid
    function up_openid_hb()
    {
        $this->pub->logq('获取支付openid ');
        $code = $this->input->get('code');
        if ($code == '') {
            echo 'code:001';
        } else {

            //当前使用的支付版本
            $hb_c = $this->config->item('hb_c');
            $hb = $this->config->item('hb');
            $a = file_get_contents('https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $hb[$hb_c]['wxAppId'] . '&secret=' . $hb[$hb_c]['wxAppSecret'] . '&code=' . $code . '&grant_type=authorization_code');
            $access_token = json_decode($a, true);

            $this->db->where('openid', $access_token['openid']);
            $q = $this->db->get_where('openid');
            if ($q->num_rows() == 0) {
                $da['mid'] = $this->session->userdata('mid');
                $da['type'] = 2;
                $da['edition'] = $hb_c;
                $da['openid'] = $access_token['openid'];
                $da['mktime'] = mktime();
                $this->db->insert('openid', $da);
            }

            if ($this->session->userdata('ls_url')) {
                redirect($this->session->userdata('ls_url'));
            } else {
                redirect('/');
            }
        }
    }
    
}


?>